package org.example.runner;

import lombok.RequiredArgsConstructor;
import org.example.domain.entity.Article;
import org.example.domain.entity.City;
import org.example.domain.entity.Spot;
import org.example.mapper.ArticleMapper;
import org.example.mapper.CityMapper;
import org.example.mapper.SpotMapper;
import org.example.utils.RedisCache;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Component
@RequiredArgsConstructor
public class ViewCountRunner implements CommandLineRunner {

    private final ArticleMapper articleMapper;

    private final CityMapper cityMapper;

    private final SpotMapper spotMapper;

    private final RedisCache redisCache;

    @Override
    public void run(String... args) throws Exception {

        List<Article> articleList = articleMapper.getAllArticlesByStatus();

        List<Spot> spotList = spotMapper.selectList(null);

        List<City> cityList = cityMapper.selectList(null);

        Map<String, Integer> articleViewCountMap = articleList.stream().collect(Collectors.toMap(article -> article.getId().toString(), article -> article.getViewCount()));

        Map<String, Integer> spotViewCountMap = spotList.stream().collect(Collectors.toMap(spot -> spot.getId().toString(), spot -> spot.getViewCount()));

        Map<String, Integer> cityViewCountMap = cityList.stream().collect(Collectors.toMap(city -> city.getId().toString(), city -> city.getViewCount()));

        redisCache.setCacheMap("ArticleViewCount",articleViewCountMap);
        redisCache.setCacheMap("SpotViewCount",spotViewCountMap);
        redisCache.setCacheMap("CityViewCount",cityViewCountMap);
        System.out.println(redisCache.getCacheMap("ArticleViewCount"));
    }
}
